package club.banyuan.hrms.servlet;

import club.banyuan.hrms.dao.EmployeeDao;
import club.banyuan.hrms.dao.impl.EmployeeDaoImpl;
import club.banyuan.hrms.domain.Department;
import club.banyuan.hrms.domain.Employee;
import com.alibaba.fastjson.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "EmployeeListServlet", urlPatterns = "/empl/list")
public class EmployeeListServlet extends HttpServlet {
    private EmployeeDao employeeDao;

    @Override
    public void init() throws ServletException {
        employeeDao = new EmployeeDaoImpl();
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        // 获取查询参数
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String sex = request.getParameter("sex");
        String phone = request.getParameter("phone");
        String email = request.getParameter("email");
        String address = request.getParameter("address");
        String education = request.getParameter("education");
        String birthday = request.getParameter("birthday");
        String departmentId = request.getParameter("departmentId");
        String departmentName = request.getParameter("departmentName");
        String positionId = request.getParameter("positionId");
        String positionName = request.getParameter("positionName");

        Employee employee = new Employee();
        employee.setId((id != null) ? Integer.parseInt(id) : null);
        employee.setName(name);
        employee.setSex(sex);
        employee.setPhone(phone);
        employee.setEmail(email);
        employee.setAddress(address);
        employee.setEducation(education);
        employee.setBirthday(birthday);
        employee.setDepartmentId(Integer.parseInt(departmentId));
        employee.setDepartmentName(departmentName);
        employee.setPositionId(Integer.parseInt(positionId));
        employee.setPositionName(positionName);

        if (id != null) {
            employeeDao.updateEmployee(employee);
        } else {
            employeeDao.addEmployee(employee);
        }

        response.sendRedirect(request.getContextPath() + "/employeeManage.jsp");
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=utf-8");

        List<Employee> employees = employeeDao.getEmployees();

        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("total", employees.size());
        resultMap.put("code", 0);
        resultMap.put("rows", employees);
        response.getWriter().println(JSONObject.toJSONString(resultMap));
    }
}
